package com.check.controller;

import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.check.pojo.Opr;
import com.check.pojo.PageObject;
import com.check.pojo.Role;
import com.check.service.AgencyService;
import com.check.service.PersonRoleService;
import com.check.service.PersonService;
import com.check.service.RoleService;

@Controller
public class PersonRoleController  extends BaseController{

	@Autowired
	@Qualifier("personService")
	private PersonService personService;
	
	@Autowired
	@Qualifier("personRoleService")
	private PersonRoleService personRoleService;
	
	@Autowired
	@Qualifier("roleService")
	private RoleService roleService;
	
	@Autowired
	@Qualifier("agencyService")
	private AgencyService agencyService;
	
	@RequestMapping("personRoleQuery.do")
	public ModelAndView personRoleQuery(HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("pageObject")PageObject pageObject){
		if(pageObject.getCurrent()==0){
			pageObject.setCurrent(1);
		}
		List<Opr> oprList=personService.findAllManager(pageObject);
		pageObject.setTotal(personService.findAllManagerCount());
		pageObject.setUrl("personRoleQuery.do");
		saveSysLog(RE_USER, LOG_QUERY, request);
		return new ModelAndView("page/personrole/personRoleList").addObject("path",request.getContextPath())
					.addObject("oprList",oprList)
					.addObject("page",pageObject.toPage());
	}
	
	@RequestMapping("toPersonRoleAdd.do")
	public ModelAndView toPersonRoleAdd(HttpServletRequest request,HttpServletResponse response){
		List<Role> roleList = roleService.findAllRole();
		String treeJson=agencyService.getOrgTreeJson();
		return new ModelAndView("page/personrole/personRoleAdd").addObject("path",request.getContextPath())
					.addObject("roleList",roleList)
					.addObject("treeJson",treeJson);
	}
	
	@RequestMapping("personRoleAdd.do")
	public void personRoleAdd(HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("opr")Opr opr){
		PrintWriter out = null;
		String flag = "0";
		
		try {
			response.setContentType("text/html;charset=gbk");
			out = response.getWriter();
			String orgs = request.getParameter("orgIds");
			String[] orgIds = orgs.split(",");
			String oprName = request.getParameter("oprName");
			opr.setOprName(URLDecoder.decode(oprName,"utf-8"));
			opr.setOrgIds(orgIds);
			saveSysLog(RE_USER, LOG_ADD, request);
			if(personRoleService.insertPersonRole(opr)){
				flag="1";
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			out.print(flag);
			if(out != null){
				out.flush();
				out.close();
			}
		}
		
	}
	
	@RequestMapping("toPersonRoleUpdate.do")
	public ModelAndView toPersonRoleUpdate(HttpServletRequest request,HttpServletResponse response){
		String oprId = request.getParameter("oprId");
		Opr opr = personRoleService.findRoleByOprId(oprId);
		String treeJson=agencyService.getOrgCheckedTreeJson(oprId);
		List<Role> roleList = roleService.findAllRole();
		return new ModelAndView("page/personrole/personRoleUpdate").addObject("path",request.getContextPath())
					.addObject("opr",opr)
					.addObject("roleList",roleList)
					.addObject("treeJson",treeJson);
	}
	
	@RequestMapping("personRoleUpdate.do")
	public void personRoleUpdate(HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("opr")Opr opr){
		PrintWriter out = null;
		String flag = "0";
		
		try {
			response.setContentType("text/html;charset=gbk");
			out = response.getWriter();
			String[] orgIds = request.getParameter("orgIds").split(",");
			String oprName = request.getParameter("oprName");
			opr.setOprName(URLDecoder.decode(oprName,"utf-8"));
			opr.setOrgIds(orgIds);
			saveSysLog(RE_USER, LOG_UPDATE, request);
			if(personRoleService.updatePersonRole(opr)){
				flag="1";
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			out.print(flag);
			if(out != null){
				out.flush();
				out.close();
			}
		}
		
	}
	
	@RequestMapping("personRoleDelete.do")
	public void personRoleDelete(HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("opr")Opr opr){
		PrintWriter out = null;
		String flag = "0";
		
		try {
			response.setContentType("text/html;charset=gbk");
			out = response.getWriter();
			String oprId=request.getParameter("oprId");
			saveSysLog(RE_USER, LOG_DELETE, request);
			if(personRoleService.deletePersonRole(oprId)){
				flag="1";
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			out.print(flag);
			if(out != null){
				out.flush();
				out.close();
			}
		}
		
	}
	
	@RequestMapping("findPersonRole.do")
	public void findPersonRole(HttpServletRequest request,HttpServletResponse response){
		PrintWriter out=null;
		String oprId=request.getParameter("oprId");
		String flag = "0";
		try{
			response.setContentType("text/html;charset=gbk");
			out=response.getWriter();
			Opr opr=personRoleService.findRoleByOprId(oprId);
			if(opr!=null){
				flag="1";
			}else{
				flag="0";
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			out.print(flag);
			if(out != null){
				out.flush();
				out.close();
			}
		}
		
	}
}
